<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryinfo.query"
            clearable
            @clear="getGoodsTableList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsTableList"></el-button>
          </el-input>
        </el-col>
        <el-col span="4">
          <el-button type="primary" @click="handleAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="GoodsTableList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column width="110" label="商品价格(元)" prop="goods_price"></el-table-column>
        <el-table-column width="100" label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column width="180" label="创建时间">
          <template slot-scope="scope">{{scope.row.add_time|dateFormat}}</template>
        </el-table-column>
        <el-table-column width="200" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>

            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleRemoveGood(scope.row.goods_id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      GoodsTableList: []
    };
  },
  created() {
    this.getGoodsTableList();
  },
  methods: {
    async getGoodsTableList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryinfo
      });
      if (res.meta.status !== 200) return this.$message.error("获取数据失败");
      this.GoodsTableList = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(pagesize) {
      this.queryinfo.pagesize = pagesize;
      this.getGoodsTableList();
    },
    handleCurrentChange(pagenum) {
      this.queryinfo.pagenum = pagenum;
      this.getGoodsTableList();
    },
    async handleRemoveGood(id) {
      try {
        const confirm = await this.$confirm("确认删除该商品吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        });
        if (confirm === "confirm") {
          const { data: res } = await this.$http.delete(`goods/${id}`);
          if (res.meta.status !== 200) return this.$message.error("操作失败");
          this.$message.success("删除成功");
          this.getGoodsTableList();
        }
      } catch (error) {
        this.$message.info("已取消删除");
      }
    },
    handleAddGoods() {
      this.$router.push("/goods/add");
    }
  }
};
</script>

<style lang="less" scoped>
</style>